package my.mall.admin.api.service

import jakarta.servlet.http.HttpServletResponse
import my.mall.common.annotation.Slf4j
import my.mall.common.annotation.Slf4j.Companion.log
import my.mall.common.util.ExcelUtils
import org.springframework.http.MediaType
import org.springframework.stereotype.Service
import java.net.URLEncoder
import java.nio.charset.StandardCharsets

@Slf4j
@Service
class CommonService {

    inline fun <reified T> exportExcel(
        title: String?,
        headers: Array<Array<String>>,
        dataset: Collection<T>,
        response: HttpServletResponse
    ) {
        val beginTime = System.currentTimeMillis()
        response.contentType = MediaType.APPLICATION_OCTET_STREAM_VALUE
        response.addHeader(
            "Content-Disposition",
            "attachment;filename=" + URLEncoder.encode(title, StandardCharsets.UTF_8) + ".xlsx"
        )
        ExcelUtils.exportExcel2007(title, headers, dataset, response.outputStream)
        log.debug("下载耗时:" + (System.currentTimeMillis() - beginTime) + "毫秒")
    }
}
